package bz.ziro.kanbe.controller.mng.page;

import java.util.Date;
import java.util.logging.Logger;
import org.slim3.controller.Controller;
import org.slim3.controller.Navigation;
import org.slim3.datastore.Datastore;
import org.slim3.util.BeanUtil;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

import bz.ziro.kanbe.dao.PageDao;
import bz.ziro.kanbe.model.Page;
import bz.ziro.kanbe.util.KeyFactory;

/**
 * ページ情報の更新
 * @author Administrator
 */
public class UpdateController extends Controller {

    @SuppressWarnings("unused")
    private static final Logger logger = Logger.getLogger(UpdateController.class.getName());

    @Override
    public Navigation run() {

        String siteKeyBuf = sessionScope("siteKey");
        String keyBuf = param("pageKey");
        //キーでページを検索
        Page page = PageDao.find(Long.valueOf(siteKeyBuf),Long.valueOf(keyBuf));

        //データを設定
        BeanUtil.copy(request, page);

        String siteTemplateBuf = (String)param("siteTemplate");
        String pageTemplateBuf = (String)param("pageTemplate");

        Key siteTemplateKey = KeyFactory.createTemplateKey(Long.valueOf(siteKeyBuf),Long.valueOf(siteTemplateBuf));
        Key pageTemplateKey = KeyFactory.createTemplateKey(Long.valueOf(siteKeyBuf),Long.valueOf(pageTemplateBuf));
        page.setSiteTemplateKey(siteTemplateKey);
        page.setPageTemplateKey(pageTemplateKey);

        UserService userService = UserServiceFactory.getUserService();
        User user = userService.getCurrentUser();

        //作成日を設定
        page.setEditDate(new Date());
        page.setEditor(user);

        Datastore.put(page);
        return null;
    }
}
